﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class guChina_shopping33 : System.Web.UI.Page
{
    public string orderId="";
    public string productname = "";
    public string pwd ="";
    public string resultmsg=""; 
    public string endtime="";
    public string ClientMsg = "";
    pay.BLL.OrderBLL orderbll = new pay.BLL.OrderBLL();
    pay.DAL.OrderDAL orderdal = new pay.DAL.OrderDAL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {    

            if (Request["orderId"] != null && Request["msg"] != null)
            {
                orderId = Request["orderId"].ToString().Split(',')[0];
                string result_msg = Request["msg"].ToString();
                if (result_msg.Equals("success"))
                {
                     GetPayMsg();
                }//if 支付状态为成功

            }//if 订单编号不为空

            else
            {
                if (Session["orderId"] != null)
                {
                    orderId = Session["orderId"].ToString();

                }
                else
                {
                    orderId = "GH" + DateTime.Now.ToString("yyyyMMddHHmmssFFF");
                }

                ClientMsg = "<ul class=\"thankscontent\">";
                ClientMsg += "<li class=\"fw\">订单号" + orderId + "的产品,待确认付款成功后,系统将自动延长您的软件使用期限！</li>";
                ClientMsg += "<li>如有疑问请拨打我们的免费服务热线：<span class=\"red fw\">4006706668</span></li>";
                ClientMsg += "</ul>"; 

            }//未完成支付


        }
    }


    private string GetProductName(int productid)
    {
        pay.BLL.ProductBLL productbll = new pay.BLL.ProductBLL();
        pay.Entity.Product product = productbll.GetElementById(productid);
        return product.Productname;
    }//获取商品名称

    /// <summary>
    /// 用于检验用户的支付是否成功
    /// </summary>
    private void GetPayMsg() 
    {
        if (orderbll.UpdateOrderSucess(orderId) == true)
        {
            pay.BLL.OrderDetailBLL ordetail_bll = new pay.BLL.OrderDetailBLL();
            pay.Entity.Web_Order orders = orderbll.GetOrderByOrderNumber(orderId);
            if (orders.SourceType == 3) //标识为炒股大赛商品
            {
                //id:66 专业版+手机版组合装
                WebService.emoneybusiness eb = new WebService.emoneybusiness();
                List<pay.Entity.OrderDetails> ordersdetail = new List<pay.Entity.OrderDetails>();
                ordersdetail = ordetail_bll.GetOrderByOrderNumber(orderId);
                string tel = "";
                foreach (var c in ordersdetail)
                {
                    if (c.Productid == 66)
                    {
                        productname = GetProductName(66); //专业版+手机版组合装
                        break;
                    }
                }

                pay.BLL.OrderDetailBLL dbl = new pay.BLL.OrderDetailBLL();
                if (Request["ext1"] == null || Request["ext1"] == "")
                {
                    Response.End();
                    return;
                }

                tel = Request["ext1"].ToString();//获取支付页面传递过来的电话号码
                string reValue = eb.QQ_AddFee(tel, 24);//使用期限只能传固定值24

                if (string.IsNullOrEmpty(reValue))
                {
                    Response.End();
                    return;
                }//if 用户还没有激活
                try
                {
                    var array = reValue.Split('|');
                    pwd = array[0]; //用户登录软件的密码
                    pwd = pwd.Replace("opPwd:", "");
                    endtime = array[1]; //软件使用到期日
                    endtime = endtime.Replace("endDate:", "");
                    resultmsg = array[2]; //激活或延长提示消息
                    resultmsg = resultmsg.Replace("msgResult:", "");

                    //string msg = "您的" + productname + "软件期限成功延长至" + endtime;
                    string msg = "您购买的" + productname + "软件" + resultmsg;
                    msg += ",专业版登录用户名为您的手机号码，密码为:" + pwd + ",服务热线4006706668.";
                    msg = msg.Replace("endDate:", "");
                    msg = msg.Replace("opPwd:", "");

                    ClientMsg = "<ul class=\"thankscontent\">";
                    ClientMsg += "<li class=\"fw\">订单号 " + orderId + "的订单产品:" + productname + " ";
                    ClientMsg += resultmsg + "<br />";
                    ClientMsg += "软件登录用户名和密码稍后将以短信的形式通知您,请注意查收！";
                    ClientMsg += "</li>";
                    ClientMsg += "<li>如有疑问请拨打我们的免费服务热线：<span class=\"red fw\">4006706668</span></li>";
                    ClientMsg += "</ul>";

                    eb.SendMsg_Yicai(tel, msg);//发送短信

                }
                catch
                {

                }

            }// if 炒股大赛产品
            else
            {
                Response.Redirect("http://pay.emoney.cn/shopping3.aspx?orderID=" + orderId);

            }//else 非炒股大赛产品
        }
        else
        {
            Response.Write("<script>alert('订单状态更新出错!');</script>");
            return;

        }
    
    }
}